package archer.application.core.service.apitenant;

import archer.application.core.cache.ApiTenantCache;
import archer.application.core.entity.apitenant.ApiTenant;
import archer.application.core.service.BaseService;
import archer.framework.security.apitenant.ApiTenantDao;
import archer.framework.utils.ValidateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author christ
 * @date 2016/6/3
 */
@Component
public class ApiTenantService extends BaseService implements ApiTenantDao {

    @Autowired
    ApiTenantCache cache;

    @Override
    public ApiTenant find(String apikey) {

        if (ValidateUtils.isEmpty(apikey)) {
            return null;
        }

        ApiTenant apiTenant = cache.getByCacheKey(apikey);

        if (isEmpty(apiTenant)) {
            apiTenant = ApiTenant.find
                    .query()
                    .where()
                    .eq("apikey", apikey)
                    .findUnique();
        }

        return apiTenant;

    }
}
